package d5_stream;

import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import static com.sun.xml.internal.messaging.saaj.packaging.mime.util.ASCIIUtility.getBytes;

public class t5 {
    @Test
    public void write() {
        try {
            FileOutputStream osw = new FileOutputStream("data01.txt");

            FileChannel channel = osw.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(1024);
            buffer.put("文档顶顶顶顶顶顶顶顶顶顶1234dddaa".getBytes());

            buffer.flip();
            channel.write(buffer);
            channel.close();


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void read() {
        try {
            FileInputStream osw = new FileInputStream("data01.txt");

            FileChannel channel = osw.getChannel();

            ByteBuffer buffer = ByteBuffer.allocate(1024);

            channel.read(buffer);
            buffer.flip();

            String s = new String(buffer.array(),0,buffer.limit(),"utf-8");
            System.out.println(s);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
